package day24;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Homework3 {
    static List<Employee> list=new ArrayList<>();
    static Scanner sc=new Scanner(System.in);
    public static void main(String[] args) throws IOException, ClassNotFoundException {

        while (true){
            System.out.println("*************");
            System.out.println("1.新增员工");
            System.out.println("2.删除员工");
            System.out.println("3.展示员工");
            System.out.println("4.退出");
            System.out.println("*************");
            System.out.print("请输入对应的编号:");
            int n=sc.nextInt();
            switch (n){
                case 1:add();break;
                case 2:delete();break;
                case 3:selete();break;
                case 4:return;
            }

        }
    }
    public static void add() throws IOException, ClassNotFoundException {
        input();
        list.add(new Employee("234","女",20));
        output();
        System.out.println("添加成功！");

    }
    public static void delete() throws IOException, ClassNotFoundException {
        System.out.print("请输入要删除的员工的编号:");
        String s=sc.next();
        input();
        list.removeIf(employee -> employee.id.equals(s));
        output();
        System.out.println("删除成功！");
    }

    public static void selete() throws IOException, ClassNotFoundException {
        input();
        for (Employee e:list){
            System.out.println(e);
        }
    }

    public static void input() throws IOException, ClassNotFoundException {
        FileInputStream fis=new FileInputStream("C:\\Users\\梅穿内裤\\Desktop\\IOTest\\aa\\employee.abc");
        ObjectInputStream ois=new ObjectInputStream(fis);
        list=(List<Employee>) ois.readObject();
        ois.close();
        fis.close();
    }
    public static void output(){
        try (FileOutputStream fos=new FileOutputStream("C:\\Users\\梅穿内裤\\Desktop\\IOTest\\aa\\employee.abc");
             ObjectOutputStream oos=new ObjectOutputStream(fos);){
             oos.writeObject(list);
             fos.close();
             oos.close();
        }catch (FileNotFoundException e){
            System.out.println("第一次使用,里面没有数据！");
        }catch (IOException e){
            e.printStackTrace();
        }
    }
}

class Employee implements Serializable {
    String id;
    String sex;
    Integer age;

    public Employee(String id, String sex, Integer age) {
        this.id = id;
        this.sex = sex;
        this.age = age;
    }

    @Override
    public String toString() {
        return "Employee{" +
                "id='" + id + '\'' +
                ", sex='" + sex + '\'' +
                ", age=" + age +
                '}';
    }
}